package com.wanchuang.hepos.help;


import androidx.fragment.app.FragmentActivity;

import com.permissionx.guolindev.PermissionX;
import com.permissionx.guolindev.callback.ExplainReasonCallback;
import com.permissionx.guolindev.request.ExplainScope;

import java.util.List;

public class PermissionHelper {
    static PermissionHelper helper;
    public static PermissionHelper getInstance(){
        if(helper==null){
            helper = new PermissionHelper();
        }
        return helper;
    }

    public interface PermissionCallBack{
        void onSuccess();
        void onFail();
    }

    /**
     *申请权限
     * @param context context 一定是activity
     * @param permissionsParams
     */
    public void getPermission(FragmentActivity context, PermissionCallBack callBack, String permissionsParams){
        PermissionX.init(context)
                .permissions(permissionsParams)
                .onExplainRequestReason(new ExplainReasonCallback() {
                    @Override
                    public void onExplainReason(ExplainScope scope, List<String> deniedList) {
                        scope.showRequestReasonDialog(deniedList, "需要您同意以下权限才能正常使用", "确定", "取消");
                    }
                }).request((allGranted, grantedList, deniedList) -> {
                    if (allGranted) {
                        callBack.onSuccess();
                    } else {
                        callBack.onFail();
                    }
                });
    }

    /**
     *  申请权限组
     * @param context context 一定是activity
     * @param groups
     */
    public void getPermissionGroup(FragmentActivity context, PermissionCallBack callBack, List<String> groups){
        PermissionX.init(context)
                .permissions(groups)
                .onExplainRequestReason(new ExplainReasonCallback() {
                    @Override
                    public void onExplainReason(ExplainScope scope, List<String> deniedList) {
                        scope.showRequestReasonDialog(deniedList, "需要您同意以下权限才能正常使用", "确定", "取消");
                    }
                }).request((allGranted, grantedList, deniedList) -> {
            if (allGranted) {
                callBack.onSuccess();
            } else {
                callBack.onFail();
            }
        });
    }


}
